//
//  PositionDetailVC.m
//  FindJob
//
//  Created by 233 on 16/3/4.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "PositionDetailVC.h"
#import "UserModel.h"
#import "onecell.h"
#import "anothercell.h"
#import "thirdCell.h"
#import "tableHeadCell.h"

#define SCREEN_WIDTH   [UIScreen mainScreen].bounds.size.width


static NSString *const kOneCell = @"onecell";
static NSString *const kAnotherCell = @"anothercell";
static NSString *const kThirdCell = @"thirdcell";
static NSString *const kHeadCell = @"headcell";


@interface PositionDetailVC ()<UITableViewDataSource,UITableViewDelegate>

@property (copy, nonatomic)NSDictionary *dataSource;
@property (weak, nonatomic) IBOutlet UITableView *tableView;


- (void)initializeUserIntefcace;  /**< 初始化用户界面 */
- (void)initializeDataSource;/**< 初始化数据源 */
@end

@implementation PositionDetailVC
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [self.view.window.layer removeAllAnimations];
    //转场动画
    CATransition *transition = [CATransition new];
    //持续时间
    transition.duration = 0.7f;
    //线性规律
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    transition.type = @"pageCurl";
    transition.subtype = kCATransitionFromLeft;

    //添加动画
    [self.view.window.layer addAnimation:transition forKey:Nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserIntefcace];
}
- (void)initializeDataSource{
  
    _dataSource = [UserModel shareUserModel].selectedDict;
}
- (void)initializeUserIntefcace{
    
    UINib *oneCellNib = [UINib nibWithNibName:NSStringFromClass([onecell class]) bundle:nil];
    [self.tableView registerNib:oneCellNib forCellReuseIdentifier:kOneCell];
    
    UINib *anotherCellNib = [UINib nibWithNibName:NSStringFromClass([anothercell class]) bundle:nil];
    [self.tableView registerNib:anotherCellNib forCellReuseIdentifier:kAnotherCell];
    
    UINib *thirdCellNib = [UINib nibWithNibName:NSStringFromClass([thirdCell class]) bundle:nil];
    [self.tableView registerNib:thirdCellNib forCellReuseIdentifier:kThirdCell];
    
    UINib *headCellNib = [UINib nibWithNibName:NSStringFromClass([tableHeadCell class]) bundle:nil];
    [self.tableView registerNib:headCellNib forHeaderFooterViewReuseIdentifier:kHeadCell];
    
 
    
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if(indexPath.row == 0){
        onecell *cell = [tableView dequeueReusableCellWithIdentifier:kOneCell];
        
        NSString *timeStr = _dataSource[@"releaseTime"];
        long long time = [timeStr longLongValue];
        NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:time / 1000.0];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *newTimeStr = [dateFormatter stringFromDate:date];
        cell.timeLabel.text = newTimeStr;
        cell.companyLabel.text = _dataSource[@"company"];
        cell.areaLabel.text = _dataSource[@"area"];
        cell.salaryLabel.text= _dataSource[@"salary"];
        cell.workExperienceLabel.text = _dataSource[@"experience"];
        cell.peopleLabel.text = _dataSource[@"count"];
        cell.scaleLabel.text = _dataSource[@"scale"];
        cell.positionDescriptionLabel.text = @"职位描述:";
        cell.academicLabel.text = _dataSource[@"education"];
        
        return cell;
    }else if (indexPath.row == 1) {
        anothercell *cell = [tableView dequeueReusableCellWithIdentifier:kAnotherCell];
        cell.decriptionLabel.text = _dataSource[@"describeJob"];
        cell.positionLabel.text = @"职位要求：";
        return cell;
    }else if (indexPath.row == 2){
        anothercell *cell = [tableView dequeueReusableCellWithIdentifier:kAnotherCell];
        cell.decriptionLabel.text = _dataSource[@"requirements"];
        cell.positionLabel.text = @"相关福利：";
        return cell;
    }else {
        thirdCell *cell = [tableView dequeueReusableCellWithIdentifier:kThirdCell];
        cell.welfareLabel.text = _dataSource[@"welfare"];
        return cell;
    }
    return nil;
}
#pragma mark - UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, CGRectGetWidth(self.view.bounds) - 20, 40)];
    label.font = [UIFont boldSystemFontOfSize:20];
    label.textAlignment = NSTextAlignmentLeft;
    label.text = _dataSource[@"position"];
    label.textColor = [UIColor brownColor];
    return label;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 250;
    }else if (indexPath.row == 1){
        CGFloat height = [self heightForString:_dataSource[@"describeJob"] fontSize:[UIFont systemFontOfSize:16] andWidth:self.tableView.bounds.size.width];
        return height + 41;
      
    }else if (indexPath.row == 2){
        CGFloat height = [self heightForString:_dataSource[@"requirements"] fontSize:[UIFont systemFontOfSize:16] andWidth:self.tableView.bounds.size.width];
        return height + 41;
    
    }else{
        CGFloat height = [self heightForString:_dataSource[@"welfare"] fontSize:[UIFont systemFontOfSize:16] andWidth:self.tableView.bounds.size.width];
        return height + 1;
   
    }
}
#pragma mark - private
- (float)heightForString:(NSString *)value fontSize:(UIFont *)font andWidth:(float)width{
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        NSDictionary *attributes = @{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle.copy};
        CGRect rectToFit = [value boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options: NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
        return rectToFit.size.height;
   
}

@end
